package ca.quicko.webapp.html;

import javax.swing.text.html.HTML;

import org.appfuse.model.Address;
import org.appfuse.model.User;

import ca.quicko.model.Issue;

/**
 * @author <a href="mailto:eric.s.landry@gmail.com">Eric S. Landry</a>
 */
public class ModelRenderer {
	public static String renderUserCoords(User user) {
		HtmlKit html = new HtmlKit();
		if (user != null) {
			html.addTagOpenNoAttr(HTML.Tag.P);
			html.add(user.getFirstName() + ' ' + user.getLastName());
			Address address = user.getAddress();
			if (address != null) {
				html.add(renderAddress(address));
			}
			html.addTagClose(HTML.Tag.P);
		}
		return html.toString();
	}

	public static String renderAddress(Address address) {
		HtmlKit html = new HtmlKit();
		html.addTagOpen(HTML.Tag.BR).addTagCloseEmpty();
		html.add(address.getAddress());
		html.addTagOpen(HTML.Tag.BR).addTagCloseEmpty();
		html.add(address.getCity() + ", " + address.getProvince());
		html.addTagOpen(HTML.Tag.BR).addTagCloseEmpty();
		html.add(address.getPostalCode() + ", " + address.getCountry());
		html.addTagOpen(HTML.Tag.BR).addTagCloseEmpty();
		return html.toString();
	}

	public static String renderIssue(Issue issue) {
		HtmlKit html = new HtmlKit();
		User user = issue.getUser();
		html.add(renderUserCoords(user));
		// TODO: Replace with renderTargetCoords(target)
		html.add("<p>John Doe (target)<br/>878asdfasdfasdfsadf7 Sussex St.<br/>Ottawa, ON<br/>K3D 2H3<br/></p>");
		html.add(wrapParagraph(issue.getTitle()));
		html.add(renderDescription(issue.getDescription()));
		return html.toString();
	}

	public static String renderDescription(String description) {
		return description.replaceAll("\\n", "<BR/><BR/>"); 
	}

	public static String wrapParagraph(String body) {
		return "<p>" + body + "</p>";
	}

	public static String wrapWithHtmlTag(String body) {
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
				+ " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
				+ "<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>"
				+ body
				+ "</body></html>";
	}
}
